package com.googlecode.jerato.library.view.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.googlecode.jerato.common.view.TableListView;
import com.googlecode.jerato.core.view.ViewFunction;
import com.googlecode.jerato.core.view.ViewParameters;
import com.googlecode.jerato.core.view.ViewTransfer;
import com.googlecode.jerato.library.SystemException;
import com.googlecode.jerato.library.SystemService;
import com.googlecode.jerato.library.view.ViewFunctionImpl;
import com.googlecode.jerato.library.view.ViewParametersImpl;
import com.googlecode.jerato.library.view.ViewTransferImpl;

public class XmlView extends ViewFunctionImpl implements ViewFunction{
	protected SAXParserFactory _parserFactory = SAXParserFactory.newInstance();
	
	protected String _resourcePath;

	public XmlView(){
		String resourceName = getClass().getName();
		_resourcePath = resourceName.replaceAll("\\.", "\\/");
		_resourcePath += ".xml";
	}

	public XmlView(String viewName){
		if(viewName == null){
			throw new IllegalArgumentException("viewName is null");
		}
		String resourceName = viewName;
		_resourcePath = resourceName.replaceAll("\\.", "\\/");
		_resourcePath += ".xml";
	}

	public void prepare(ViewTransfer trans, ViewParameters input, ViewParameters output){
		
	}

	public void execute(ViewTransfer trans, ViewParameters input, ViewParameters output){
		SAXParser saxParser = null;
		try {
			saxParser = _parserFactory.newSAXParser();
		} catch (ParserConfigurationException e1) {
			throw new SystemException("sax initialize failed.", e1);
		} catch (SAXException e1) {
			throw new SystemException("sax initialize failed.", e1);
		}
		
		XmlViewParser parser = new XmlViewParser();

		InputStream inputStream = SystemService.getInstance().getResourceAsStream(_resourcePath);

		if(inputStream == null){
			throw new SystemException("resource not found. resourcePath=" + _resourcePath);
		}
		try {
			saxParser.parse(inputStream, parser);
		} catch (IOException e) {
			throw new SystemException("sax failed.", e);
		} catch (SAXException e) {
			throw new SystemException("sax failed.", e);
		}
//		List list = parser._itemList;
//		StringMap map = parser.getSettings();
		XmlViewNode rootNode = parser.getRootNode();

//		trans.println("<html>");
//		trans.println("<head>");
//		if(map.getString("title") != null){
//			trans.println("<title>");
//			trans.println(map.getString("title"));
//			trans.println("</title>");
//		}
//		trans.println("</head>");
//		trans.println("<body>");
		
		prepare(trans, input, output);

		StringWriter writer = new StringWriter();
//		rootNode.render(writer, output);
		rootNode.render(writer, trans, input, output);
		writer.flush();
		trans.println(writer.toString());
//		trans.println(rootNode.toString());
//		for(Iterator itr = list.iterator(); itr.hasNext(); ){
//			String item = (String)itr.next();
//			input.print(item);
//		}
//		trans.println("</body>");
//		trans.println("</html>");
	}

	public static void main(String[] args) {
		SystemService.staticInitialize();
		XmlView view = new TableListView();
		ViewParametersImpl input = new ViewParametersImpl();
		input.setString("message", "hello");
//		ArrayList list = new ArrayList();
//		HashMap map = new HashMap();
//		map.put("table_name", "dual");
//		list.add(map);
//		input.put("tableList", list);
		ViewParametersImpl output = new ViewParametersImpl();
		ViewTransferImpl trans = new ViewTransferImpl();
		view.call(trans, input, output);
		String buffer = trans.getStringBuffer();
		System.out.println("----------");
		System.out.println(buffer);
		System.out.println("----------");
		
	}


}
